<template>
  <view class="authorIdentIyPage">
    <view class="authorIdentIy">
      <form @submit="formSubmit">
        <view class="form">
          <view class="tips"
            >万户健康项目是面向慢病会员提供的大病保障服务，名额有限，需参与者实名登记，请提供真实身份信息</view
          >
          <view class="content marginTop-40 paddingBotNone">
            <view class="inputBox marginTop20">
              <span class="name lineHeight86">姓名</span>
              <input
                v-model="formData.name"
                class="uni-input lineHeight86"
                name="name"
                placeholder="请输入姓名"
                :disabled="isAuthen"
                placeholder-class="phcolor"
              />
              <view v-show="formData.name && !isAuthen" class="clearInp" @click.stop="clearInpVal('name')"
                ><img :src="closeIcon"
              /></view>
              <view class="clear"></view>
            </view>
            <view class="inputBox">
              <span class="name lineHeight86">身份证</span>
              <input
                v-model="formData.idCard"
                class="uni-input lineHeight86"
                name="idCard"
                placeholder="请输入身份证号"
                placeholder-class="phcolor"
                :disabled="isAuthen"
              />
              <view v-show="formData.idCard && !isAuthen" class="clearInp" @click.stop="clearInpVal('idCard')"
                ><img :src="closeIcon"
              /></view>
              <view v-show="formData.idCard && isAuthen" class="idAuthen" @click.stop="clearInpVal('idCard')"
                >实名</view
              >
              <view class="clear"></view>
            </view>
          </view>
        </view>
        <view class="uni-btn-v">
          <button form-type="submit">验证身份</button>
        </view>
      </form>
    </view>
  </view>
</template>

<script>
import { getRealNameResult, getPatientIdByIdCard } from '@/api/join.js';
import { getPatientId } from '@/utils/auth.js';
let graceChecker = require('../../common/graceChecker.js');
export default {
  data() {
    return {
      formData: {
        name: '',
        idCard: ''
      },
      userCheckPass: true,
      patientId: getPatientId()
    };
  },
  onLoad() {},
  methods: {
    async formSubmit(e) {
      // 基础校验，字符长度，格式
      // 定义表单规则
      let rule = [
        {
          name: 'name',
          checkType: 'string',
          checkRule: '1,4',
          errorMsg: '姓名应为1-4个字符'
        },
        {
          name: 'idCard',
          checkType: 'idcard',
          checkRule: '',
          errorMsg: '请填写正确身份证号'
        }
      ];
      let formData = e.detail.value;
      let checkRes = graceChecker.check(formData, rule);
      if (!checkRes) {
        uni.showToast({ title: graceChecker.error, icon: 'none' });
        return false;
      }
      // ② 检验身份证号是否已存在
      const isBeing = await this.checkUserBeing();
      if (isBeing) {
        return false;
      }
      // ① 验证身份是否可通过实名校验
      const isTrue = await this.checkUserReal();
      if (!isTrue) {
        return false;
      }
      this.$wxPromise.navigateTo({
        url: '/pages/join/form'
      });
    },
    async checkUserReal() {
      const realNameData = {
        patientId: this.patientId || '',
        name: this.formData.name,
        idCard: this.formData.idCard
      };
      this.userCheckPass = false;
      await getRealNameResult(realNameData).then((res) => {
        if (res.code === 0 || res.code === 2) {
          this.userCheckPass = true;
          return true;
        } else {
          wx.showModal({
            title: '认证失败',
            content: res.message,
            showCancel: false,
            success: function (res) {}
          });
        }

        return false;
      });
      if (this.userCheckPass) {
        return true;
      }
      return false;
    },
    async checkUserBeing() {
      const data = {
        phone: '',
        idCard: this.formData.idCard
      };
      let isBeing = await getPatientIdByIdCard(data).then((res) => {
        if (res.code === 0 && res.data && res.data !== this.patientId) {
          wx.showModal({
            title: '认证失败',
            content:
              '身份信息与其他会员信息重复，请重新输入。如有疑问，请关注“万户健康”公众号联系在线客服，或拨打：400-010-1516。',
            showCancel: false,
            success: function (res) {}
          });
          return true;
        }
        return false;
      });
      return isBeing;
    }
  }
};
/*
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
*/
</script>
<style lang="scss">
.authorIdentIy {
  .tips {
    margin: 24rpx 0;
    color: rgba(0, 0, 0, 0.55);
    font-size: 28rpx;
  }
  .form {
    padding: 10rpx 30rpx;
    border-radius: 20rpx;
    background-color: #fff;
    margin: 24rpx 24rpx 24rpx 24rpx;
    box-shadow: 0px 12px 12px #e8e8e8;

    .inputBox {
      border-bottom: 1px solid #e8e8e8;
      font-size: 28rpx;
      position: relative;
    }
    .inputBox:last-child {
      border: none;
    }
    .clearInp {
      position: absolute;
      right: 0;
      top: 25rpx;
      z-index: 99;
      cursor: pointer;
    }
    .clearInp img {
      width: 48rpx;
      height: 48rpx;
    }
  }
  .idAuthen {
    position: absolute;
    right: 80rpx;
    top: 25rpx;
    z-index: 99;
    cursor: pointer;
    background: rgba(0, 0, 0, 0.3);
    width: 64rpx;
    height: 32rpx;
    line-height: 32rpx;
    border-radius: 4rpx;
    text-align: center;
    color: #fff;
    font-size: 24rpx;
  }
  .inputBox .title {
    float: left;
  }
  .inputBox .name {
    float: left;
    width: 140rpx;
  }
  .inputBox .uni-input {
    float: right;
    width: 450rpx;
    color: rgba(0, 0, 0, 0.55);
  }
  .clear {
    clear: both;
    overflow: hidden;
    zoom: 1;
    height: 0px;
  }
  .marginTop20 {
    margin-top: 20rpx;
  }
  .city {
    color: rgba(0, 0, 0, 0.55);
    font-size: 28rpx;
    position: relative;
  }
  .lineHeight86 {
    height: 86rpx;
    line-height: 86rpx;
  }
  .size28 {
    font-size: 28rpx;
  }
  .uni-btn-v {
    margin-top: 60rpx;
    padding-bottom: $uni-fixed-button-parent-padding-bottom;
  }
  .uni-btn-v button {
    width: 702rpx;
    height: 88rpx;
    border: 2rpx solid rgba(0, 0, 0, 0);
    background: linear-gradient(236deg, #fe894d 0%, #ef3931 100%);
    opacity: 1;
    border-radius: 44rpx;
    color: #fff;
    font-size: 36rpx;
  }
}
</style>
